package com.dubbo.rpc;

import com.dubbo.rpc.domain.RpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author liuxin
 * @date 2021/10/10 10:30
 */
public class RpcClientHandler extends SimpleChannelInboundHandler<RpcResponse> {

    private final ConcurrentHashMap<String, RpcResponse> responseMap;

    public RpcClientHandler(ConcurrentHashMap<String, RpcResponse> responseMap) {
        this.responseMap = responseMap;
    }


    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, RpcResponse rpcResponse) throws Exception {
        System.err.println(10);
        responseMap.put(rpcResponse.getRequestId(), rpcResponse);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
